package leetcode.other.p34;

import java.util.Arrays;

class Solution {
    int lowerBound(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] < target) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }

    public int[] searchRange(int[] nums, int target) {
        int start = lowerBound(nums, target);
        if(start == nums.length || nums[start] != target) {
            return new int[]{-1, -1};
        }
        int end = lowerBound(nums, target + 1) - 1;

        return new int[] {start, end};
    }

    public static void main(String[] args) {
        //示例 1：
        //输入：nums = [5,7,7,8,8,10], target = 8
        //输出：[3,4]
        System.out.println(Arrays.toString(new Solution().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8)));
        //示例 2：
        //输入：nums = [5,7,7,8,8,10], target = 6
        //输出：[-1,-1]
        System.out.println(Arrays.toString(new Solution().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6)));
        //示例 3：
        //输入：nums = [], target = 0
        //输出：[-1,-1]
        System.out.println(Arrays.toString(new Solution().searchRange(new int[]{}, 0)));
        // System.out.println(new Solution().lowerBound(new int[]{}, 0));
        // System.out.println(new Solution().lowerBound(new int[]{5, 7, 7, 8, 8, 10}, 8));
    }
}